package com.wisdytech.linkdcs.factory.action;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.factory.model.ProductionLine;
import com.wisdytech.linkdcs.factory.model.Shop;
import com.wisdytech.linkdcs.factory.service.TmProductionLineService;
import com.wisdytech.linkdcs.factory.vo.ProductionLineVO;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * <p>
 * ClassName: FactoryAction
 * </p>
 * <p>
 * Description: 产线管理页面的action层
 * </p>
 * <p>
 * Author: chenbin
 * </p>
 * <p>
 * Date: 2018-5-3
 * </p>
 */
@Controller
@RequestMapping("/factory/product")
public class ProductionLineAction {

    /**
     * 注入厂区tmProductionLineService
     */
    @Autowired
    @Qualifier("tmProductionLineService")
    private TmProductionLineService tmProductionLineService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "factoryModel/productionLine";
    }
    
    /**
     * 分页条件查询产线管理列表信息
     * @param factoryCode 工序编码
     * @param factoryName 工序名称
     * @param filter 分页参数
     * @return 厂区管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listProductionLineByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<ProductionLineVO> listProductionLineByFilter(String lineCode, String lineName, Filter filter) throws IOException{
    	PageInfo<ProductionLine> productionLinePage =this.tmProductionLineService.listProductionLineByFilter(lineCode, lineName, filter);
        //转换为前台结果集
        List<ProductionLine> productionLineList=productionLinePage.getList();
        List<ProductionLineVO> productionLineVOList=new ArrayList<ProductionLineVO>();
        for(ProductionLine productionLine:productionLineList){
        	productionLineVOList.add(DataTransformUtil.changeProductionLineVO(productionLine));
        }
        return new StandardTableResult<ProductionLineVO>(productionLinePage.getTotal(),productionLineVOList);
    }
    
    /**
     * <p>Description: 保存产线对象</p> 
     * @param model 产线对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveProductionLine/save.mvc")
    @ResponseBody
    public StandardResult saveProductionLine(ProductionLine productionLine, HttpSession session,HttpServletRequest request) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(productionLine.getPkId())) {
            tmProductionLineService.addProductionLine(productionLine, loginName);
        }else {
            tmProductionLineService.updateProductionLine(productionLine, loginName);
        }
        return StandardResult.getSuccessResult();
    }
    
    /**
     * 删除产线信息
     * @param pkId 设备标识id
     * @return 结果对象
     */
    @RequestMapping(value = "/delProductionLineById/del.mvc")
    @ResponseBody
    public StandardResult delProductionLineById(String pkId) {
    	tmProductionLineService.delProductionLineById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询产线信息列表</p> 
     * @return 产线信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/getProductById/get.mvc")
    @ResponseBody
    public ProductionLine getProductById(String pkId){
        return tmProductionLineService.getProductById(pkId);
    }
    
    /**
     * <p>Description:查询产线信息列表</p> 
     * @return 产线信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/listAllProduct/get.mvc")
    @ResponseBody
    public List<ProductionLine> listAllProduct(@ModelAttribute("lineName") String search) throws IOException{
        return tmProductionLineService.listAllProduct(search);
    }

    /**
     *
     * @param factoryId 厂区id
     * @return StandardResult
     */
    @RequestMapping(value = "/listAllLinesByFactoryId/get.mvc")
    @ResponseBody
    public StandardResult listAllLinesByFactoryId(String factoryId) {
        List<ProductionLine> list = this.tmProductionLineService.listAllLine(factoryId);
        return StandardResult.getSuccessResult(list);
    }
}
